import useUserStore from '@/stores/modules/user'
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', component: () => import('@/views/Layout/Layout.vue'), redirect: '/article/channel' },
    {
      path: '/layout', component: () => import('@/views/Layout/Layout.vue'), children: [
        { path: '/article/channel', component: () => import('@/views/article/ArticleChannel.vue') },
        { path: '/article/manager', component: () => import('@/views/article/ArticleManager.vue') },
        { path: '/user/avatar', component: () => import('@/views/user/userAvatar.vue') },
        { path: '/user/password', component: () => import('@/views/user/userPassword.vue') },
        { path: '/user/profile', component: () => import('@/views/user/userProfile.vue') }
      ]
    },
    { path: '/login', component: () => import('@/views/login/Login.vue') }
  ],
})

// 路由前置守卫
router.beforeEach((to) => {
  const token = useUserStore().userInfo.token
  if (!token && to.path !== '/login') {
    return '/login'
  }
})

export default router
